package cn.dengta.webapp.trade.entity;

import java.util.Date;
import java.util.List;
import javax.annotation.Nullable;

import cn.dengta.common.model.BizFlag;
import cn.dengta.common.model.Status;
import cn.dengta.common.util.Common;
import cn.dengta.context.model.Language;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.base.entity.I18nContent;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.*;
import org.apache.commons.lang3.BooleanUtils;

/**
 * 礼物信息
 *
 * @author lgy
 * @since 2022-03-01
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("tr_gift")
public class Gift extends BizEntity {

	/** 标识：lvl */
	public static final long BIZ_FLAG_CHAT = 1L;
	/** 标识：直播 */
	public static final long BIZ_FLAG_LIVE = 1L << 1;

	/** 图标 */
	private String icon;
	/** 动画效果 */
	private String svga;
	/** 名称 */
	private String name;
	/** 钻石数 */
	private Integer coin;
	/** 链接 */
	private String url;
	/** 分类 */
	private Long giftCategoryId;
	/** 标签 */
	private String iconTag;
	/** 是否显示avg 1-是 0-否 */
	private Integer isAvgView;
	/** 1:全屏 2：半屏 */
	private Integer actIsScreen;
	/** 是否需要VIP：0=否；1=VIP；2=SVIP */
	private Integer vipLevel;
	/** 添加用户ID */
	protected Long addUserId;
	/** 排序 */
	private Integer sort;
	/** 礼物发送次数 */
	private Integer sendCount;
	/** 礼物业务标识：1v1，直播 */
	private long giftFlag;
	/** 便捷礼物 */
	private Integer quick;

	/** 分包 */
	private String wesKits;

	public transient Integer chatOpen, liveOpen;
	protected transient I18nContent resolvedLanguageCfg;

	public Gift init(Gift input, Date now) {
		this.icon = X.expectNotEmpty(input.icon, icon);
		this.name = X.expectNotNull(input.name, name, "");
		this.coin = X.expectNotNull(input.coin, coin);
		this.svga = StringUtil.toString(input.svga);
		this.url = StringUtil.toString(input.url);
		this.giftCategoryId = X.expectNotNull(input.giftCategoryId, giftCategoryId);
		this.iconTag = StringUtil.toString(input.iconTag);
		this.isAvgView = X.expectNotNull(input.isAvgView, isAvgView, 1);
		this.actIsScreen = X.expectNotNull(input.actIsScreen, actIsScreen, 1);
		this.vipLevel = X.expectNotNull(input.vipLevel, 0);
		this.quick = X.expectNotNull(input.quick, 0);
		this.wesKits = X.expectNotNull(input.wesKits, wesKits);
		this.sort = X.expectNotNull(input.sort, sort, 0);
		this.chatOpen = X.expectNotNull(input.chatOpen, BooleanUtils.toInteger(BizFlag.hasFlag(giftFlag, BIZ_FLAG_CHAT)));
		this.liveOpen = X.expectNotNull(input.liveOpen, BooleanUtils.toInteger(BizFlag.hasFlag(giftFlag, BIZ_FLAG_LIVE)));
		toggleBizFlag();
		super.initTime(now);
		super.init(input.getStatus(), input.state);
		return this;
	}

	private void toggleBizFlag() {
		this.giftFlag = BizFlag.WritableBizFlag.toggleFlag(giftFlag, BIZ_FLAG_CHAT, Status.asResult(this.chatOpen));
		this.giftFlag = BizFlag.WritableBizFlag.toggleFlag(giftFlag, BIZ_FLAG_LIVE, Status.asResult(this.liveOpen));
	}

	/** 1v1类型 */
	public boolean hasChatFlag() {
		return BizFlag.hasFlag(giftFlag, BIZ_FLAG_CHAT);
	}

	/** 直播类型 */
	public boolean hasLiveFlag() {
		return BizFlag.hasFlag(giftFlag, BIZ_FLAG_LIVE);
	}

	/** 快捷礼物 */
	public boolean forQuick() {
		return Status.asResult(quick);
	}

	/** 是否需要VIP */
	public boolean needVip() {
		return vipLevel != null && vipLevel > 0;
	}

	@Nullable
	public I18nContent getResolvedLanguageCfg() {
		I18nContent cfg = resolvedLanguageCfg;
		if (cfg == null) {
			resolvedLanguageCfg = cfg = StringUtil.isEmpty(name) ? null : I18nContent.deserialize(name);
		}
		return cfg;
	}

	public void updateName(I18nContent config) {
		this.name = I18nContent.serialize(this.resolvedLanguageCfg = config);
	}

	public I18nContent updateResolvedLanguageCfg(String config) {
		this.resolvedLanguageCfg = I18nContent.deserialize(this.name = config);
		return getResolvedLanguageCfg();
	}

	public String i18nName() {
		return X.map(getResolvedLanguageCfg(), I18nContent::get);
	}

	public String i18nNameOrDefault() {
		final String name = X.map(getResolvedLanguageCfg(), I18nContent::get);
		return StringUtil.isEmpty(name) ? i18nName(Language.en) : name;
	}

	public String i18nName(Language language) {
		return X.map(getResolvedLanguageCfg(), l -> l.get(language));
	}

	public static final String ICON = "icon";
	public static final String NAME = "name";
	public static final String COIN = "coin";
	public static final String URL = "url";
	public static final String GIFT_CATEGORY_ID = "gift_category_id";
	public static final String ICON_TAG = "icon_tag";
	public static final String IS_AVG_VIEW = "is_avg_view";
	public static final String SVGA = "svga";
	public static final String ACT_IS_SCREEN = "act_is_screen";
	public static final String ADD_USER_ID = "add_user_id";
	public static final String VIP_LEVEL = "vip_level";
	public static final String QUICK = "quick";
	public static final String GIFT_FLAG = "gift_flag";
	public static final String SORT = "sort";
	public static final String SEND_COUNT = "send_count";
	public static final String SCOPE = "scope";
	public static final String WES_KITS = "wes_kits";

}
